package com.jadesoft.stock.util;

import org.apache.log4j.Logger;

public class FileUtil {
	private static Logger log = Logger.getLogger(FileUtil.class);

	public static String getRootDir() {
		String result = FileUtil.class.getResource("FileUtil.class").toString();
		log.debug("file path:" + result);
		int index = result.indexOf("WEB-INF");
		if (index == -1) {
			index = result.indexOf("bin");
		}
		result = result.substring(0, index);
		if (result.startsWith("zip")) { // 当class文件在war中时，返回"zip:D:/ ..."样的路径
			result = result.substring(4);
		} else if (result.startsWith("file")) { // 当class文件在class文件中时，返回"file:/F:/ ..."样的路径
			result = result.substring(6);
		} else if (result.startsWith("jar")) { // 当class文件在jar文件中时，返回"jar:file:/F:/ ..."样的路径
			result = result.substring(10);
		}
		if (result.endsWith("/"))
			result = result.substring(0, result.length() - 1);// 不包含最后的"/"
		result = result.replace("%20", " "); // Window下替换空格
		return result;
	}

	public static void main(String[] args) {
		System.out.println(FileUtil.getRootDir());
	}
}
